// pages/commonInfo/repairInfo/idnex.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), common_Js = require("../../../../component/common.js");
import Dialog from '../../../../component/vant/dialog/dialog';
import Toast from '../../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    record:{},
    record_id:0,
    isLoading:true,
    hasData:false,
    fileList1: [],
    submitState:false,
    isShowPopup:false,
    workerNames:[],
    worker_id:0,
    workers:[],
    workerName:'',
    content:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.data.record_id = parseInt(options.id);
      this.onHttpInit()
    }
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/repair/record/detail', {
      record_id:that.data.record_id,
    }, function (res) {
      that.data.isNewReq = 0;
      if (res.data.code == 0) {
        that.setData({
          record:res.data.data,
          workerNames:res.data.data.workerNames,
          workers:res.data.data.workers,
        })
      }
      that.setData({
         isLoading: false, 
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false, })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  
  /**
   * 处理报修操作
   */
  submitOpToWorker:function(){
    var that = this;
    if(that.data.worker_id == 0){
        Toast.fail('请选择维修师傅');
        return;
    }
    
    if (that.data.content == ''){
        Toast.fail('情况说明内容不能为空');
        return;
    }

    var content = this.trim(that.data.content);
    console.log('content',content)
    console.log('content.length', content.length)
    
    if(that.data.submitState){
      Toast.fail('正在提交...');
      return;
    }
    that.data.submitState = true;
    that.setData({submitState : true })

    fetchApi.httpPost('/repair/record/setWorker', {
      record_id: that.data.record_id,
      worker_id:that.data.worker_id,
      content: content,
    }, function (res) {
      console.log("指派维修工处理返回数据", res)
      if (res.data.code == 0) {
        that.data.submitState = false;
        that.setData({submitState : false })
        Toast.success(res.data.msg);
        setTimeout(function () {
          wx.navigateBack({
            delta: -1,
          })
        }, 1000)
      }else{
        that.data.submitState = false;
        that.setData({submitState : false })
        Toast.fail( res.data.msg);
        return;
      }
    },function(){})
  },
  /**
   * 处理报修操作
   */
  submitOp:function(){
    var that = this;
    var content = this.trim(that.data.content);
    console.log('content',content)
    console.log('content.length', content.length)
    
    if (content.length <= 0){
        Toast.fail('情况说明内容不能为空');
        return;
      }
    if(that.data.submitState){
      Toast.fail('正在提交...');
      return;
    }
    that.data.submitState = true;
    that.setData({submitState : true })

    fetchApi.httpPost('/repair/record/handle', {
      record_id: that.data.record_id,
      content: content,
    }, function (res) {
      console.log("上报处理返回数据", res)
      if (res.data.code == 0) {
        that.data.submitState = false;
        that.setData({submitState : false })
        Toast.success(res.data.msg);
        if (that.data.fileList1.length > 0) {
          for (var index in that.data.fileList1) {
            wx.uploadFile({
              url: fetchApi.getPlatformUrl() + '/repair/record/uploadImg',
              filePath: that.data.fileList1[index].url,
              name: 'file',
              formData: { 
                record_id: that.data.record_id,
                user_id: app.getStorageSync("user_id"),
                img_type:1,
              },
              success: function (res) {
                console.log('uploadFile success res', res)
              }, fail: function (res) {
                console.log('uploadFile fail res', res)
              }
            })
          }
        }
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 1000)
      }else{
        that.data.submitState = false;
        that.setData({submitState : false })
        Toast.fail( res.data.msg);
        return;
      }
    },function(){})
  },
  preImg: function (t) {
    console.log("preSunImg", t)
    var a = t.currentTarget.dataset.imageindex, e = t.currentTarget.dataset.imgdata || [];
    var c = [];
    for (var index in e) {
      c.push(e[index])
    }
    common_Js.preImg(a, c)
  },
  deleteImg:function(res){
    console.log('删除图片', res)
    var index = res.currentTarget.dataset.index;
    this.data.fileList1 = [];
    this.setData({
      fileList1: this.data.fileList1
    });
  },
  afterRead(event) {
    console.log('afterRead',event)
    const { file, name } = event.detail;
    console.log('afterRead name',name)
    console.log('afterRead file',file)
    const fileList = this.data[`fileList${name}`];
    
    this.setData({ [`fileList${name}`]: fileList.concat(file) });
    
    console.log('afterRead fileList',fileList)
    this.data.fileList1.concat(file)
    console.log('fileList',this.data.fileList1)
  },
  bindInput: function (t) {
    console.log("情况说明输入", t)
    this.data.content = t.detail.value || "";
  },
  trim: function (t) {
    console.log("trim",t);
    return t.replace(/(^\s*)|(\s*$)/g, "");
  },
  oversize() {
    wx.showToast({ title: '文件超出大小限制', icon: 'none' });
  },
  onClickWorker() {
    this.setData({
      isShowPopup: !this.data.isShowPopup
    })
  },
  onChangeWorker(e){
    console.log('onChangeWorkert',e)
    var index = e.detail.index;
    console.log('this.data.workerName',this.data.workerNames[index])
    this.data.worker_id = this.data.workers[index].id; 
    console.log('worker_id',this.data.worker_id);
    this.data.workerName = this.data.workerNames[index]; 
    this.setData({
      isShowPopup: !this.data.isShowPopup,
      workerName: this.data.workerName
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})